package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.beans.ScheduleVO;
import com.icss.daos.ScheduleDao;

public class ScheduleServlet extends HttpServlet {

	/**
	 * 日程安排
	 */
	public ScheduleServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
        System.out.println("程序在ScheduleServlet 的get() 方法");
	
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		ScheduleDao dao=null;
		System.out.println("程序在ScheduleServlet 的doPost() 方法");
           request.setCharacterEncoding("utf-8");
           response.setCharacterEncoding("utf-8");
           //获得隐藏域
           String agenda=request.getParameter("agenda");
           if(agenda.equals("add")){
        	   //获得页面表单的文本值
        	   Date txtSendTimeMIN=null;
        	   Date txtSendTimeMAX = null;
               String personName=request.getParameter("txtPersonName");
               if(request.getParameter("txtSendTimeMIN")!=null&&request.getParameter("txtSendTimeMIN")!=""){
               txtSendTimeMIN=Date.valueOf(request.getParameter("txtSendTimeMIN"));
               }
               if(request.getParameter("txtSendTimeMAX")!=null&&request.getParameter("txtSendTimeMIN")!=""){
               txtSendTimeMAX=Date.valueOf(request.getParameter("txtSendTimeMAX"));
               }
               //System.out.println("开始时间是"+txtSendTimeMIN);
               String selectCalType=request.getParameter("selectCalType");
               String txtContent=request.getParameter("txtContent");
               String txtRemark=request.getParameter("txtRemark");
        	   dao=new ScheduleDao();
        	   dao.insertSchedule(personName,txtSendTimeMIN,txtSendTimeMAX,selectCalType,txtContent,txtRemark);     	   
        	   
           }
           if(agenda.equals("query")){
               //获得查询页面表单的文本值
        	   List<ScheduleVO> scheduleList=new ArrayList<ScheduleVO>();
        	   Date txtSendTimeMIN=null;
        	   Date txtSendTimeMAX = null;
               String personName=request.getParameter("txtPersonName");
               if(request.getParameter("txtSendTimeMIN")!=null&&request.getParameter("txtSendTimeMIN")!=""){
               txtSendTimeMIN=Date.valueOf(request.getParameter("txtSendTimeMIN"));
               }
               if(request.getParameter("txtSendTimeMAX")!=null&&request.getParameter("txtSendTimeMIN")!=""){
               txtSendTimeMAX=Date.valueOf(request.getParameter("txtSendTimeMAX"));
               }
        	  
               String selectCalType=request.getParameter("selectCalType");
               String txtRemark=request.getParameter("txtRemark");
        	   dao=new ScheduleDao();
        	   scheduleList=dao.selectSchedule(txtSendTimeMIN,txtSendTimeMAX,selectCalType,txtRemark,personName);
        	  
        	   if(scheduleList!=null){	  
        		   request.setAttribute("scheduleVo", scheduleList);
       			   String url="/page/office/richeng/edit_agenda.jsp";
       			   RequestDispatcher rd=request.getRequestDispatcher(url);
       			   rd.forward(request, response);   
        	   }else{System.out.println("没有匹配的数据");}
        	    
        	   
           }
           if(agenda.equals("edit")){
        	   
        	   
           }
           
       
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
